package com.iltgcl.echovoicedemo.speaker

import android.media.AudioManager
import androidx.media.AudioAttributesCompat
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat
import com.iltgcl.echovoice.client.IEchoVoice
import timber.log.Timber

/**
 * 用于导航或通知类应用：短暂获取音频焦点，播放完成后释放音频焦点
 */
class NavigationSpeaker(
    audioManager: AudioManager,
    echoVoice: IEchoVoice ) : EchoSpeakerBase(audioManager, echoVoice, IEchoVoice.USAGE_NOTIFICATION) {

    private val focusChangeListener = AudioManager.OnAudioFocusChangeListener { focusChange ->
        Timber.d("[onAudioFocusChange]$usage, $focusChange")
    }

    private val focusRequest = AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT)
        .setAudioAttributes(
            AudioAttributesCompat.Builder()
                .setContentType(AudioAttributesCompat.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributesCompat.USAGE_NOTIFICATION)
                .build())
        .setOnAudioFocusChangeListener(focusChangeListener)
        .build()


    override fun requestAudioFocus(audioManager: AudioManager): Int {
        return AudioManagerCompat.requestAudioFocus(audioManager, focusRequest)
    }

    override fun abandonAudioFocus(audioManager: AudioManager): Int {
        return AudioManagerCompat.abandonAudioFocusRequest(audioManager, focusRequest)
    }

    override var focusLossTransientPolicy = IEchoSpeaker.AUDIO_FOCUS_LOSS_TRANSIENT_POLICY_NONE

}